package org.example;

public class Main {
    public static void main(String[] args) {
        System.out.println("Hello world!");
    }
}
class ListNode {
      int val;
      ListNode next;
      ListNode() {}
      ListNode(int val) { this.val = val; }
      ListNode(int val, ListNode next) { this.val = val; this.next = next; }
}
class Solution {
    public ListNode addTwoNumbers(ListNode l1, ListNode l2) {
        ListNode cur1 = l1, cur2 = l2;
        ListNode dummyHead = new ListNode(0);//虚拟头节点
        ListNode cur3 = dummyHead;
        int t = 0;
        while(cur1 != null || cur2 != null || t != 0){
            if(cur1 != null)
                t += cur1.val;
            if(cur2 != null)
                t += cur2.val;
            cur3.next = new ListNode(t % 10);
            t /= 10;
            cur3 = cur3.next;
            if(cur1 != null)
                cur1 = cur1.next;
            if(cur2 != null)
                cur2 = cur2.next;
        }

        return dummyHead.next;
    }
}